
from utils.db import db1


class Acl():

    def __init__(self):
        pass

    # 给用户添加权限
    def set_resource(self,uid,reslist):
        for i in reslist:
            sql = "insert into user_resource(userid,resourceid) values (%d,%d)"%(int(uid),int(i))
            db1.update(sql)


    #修改权限
    def update_resource(self,uid,reslist):
        #先删除原来的在重新添加
        sql = "delete from user_resource where uid=%d"%(int(uid))
        db1.update(sql)
        self.set_resource(uid,reslist)


    # 删除某个权限
    def delete_resource(self,uid,resourceid):
        sql = "delete from user_resource where resourceid=%d"%(int(resourceid))
        db1.update(sql)


    #获取页面资源列表
    def get_page_reslist(self,uid):
        sql = "select r.url from resource as r left join user_resource as ur on r.id=ur.resourceid where ur.userid=%d and r.type=1"%(int(uid))

        res = db1.find_all(sql)
        list = []
        for i in res:
            list.append(i['url'])
        return list


    #获取接口资源列表
    def get_interface_resource(self,uid):
        sql = "select * from resource as r left join user_resource as ur on r.id=ur.resourceid where ur.userid=%d and r.type=2"%(int(uid))
        res = db1.find_all(sql)
        list = []
        for i in res:
            list.append(i['url'])
        return list

    def get_recourse(self,uid):
        sql = "select r.name,r.url,r.id,pr.id as pid,pr.name as pname from user_resource as ur left join resource as r on ur.resourceid=r.id left join resource as pr on pr.id=r.pid where ur.userid=%d and r.type=1;" % int(uid)
        res = db1.find_all(sql)
        return res


acl = Acl()

# print(acl.get_page_reslist(1))
# print(acl.get_interface_resource(1))














